package com.eportal.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


/**
 * 服务器端http请求工具类
 */
public class HttpHelper {

	/**
	 * 发起get请求
	 * <p>
	 */
	public static String doGet(String url){
	
		String 	rs = executeGet(url);
		return rs;
	}

	/**
	 * access_token 接口直接调用，其它调用doGet
	 */
	private static String executeGet(String url){
		try {
			HttpGet httpGet = new HttpGet(url);
			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse response = httpclient.execute(httpGet);
			String resultContent = new Utf8ResponseHandler()
					.handleResponse(response);
			return resultContent;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 发起post请求
	 */
	public static String doPost(String url, String body) {
		try {
			HttpPost httpPost = new HttpPost(url);
			StringEntity entity = new StringEntity(body, "UTF-8");
			httpPost.setEntity(entity);
			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse response = httpclient.execute(httpPost);
			String resultContent = new Utf8ResponseHandler()
					.handleResponse(response);
			return resultContent;

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	

	
	/**
	 * 发起post请求
	 */
	public static String doPost(String url, HttpEntity entity) {
		try { 
			HttpPost httpPost = new HttpPost(url);
			httpPost.setEntity(entity);
			HttpClient httpclient = new DefaultHttpClient();
			HttpResponse response = httpclient.execute(httpPost);
			String resultContent = new Utf8ResponseHandler()
					.handleResponse(response);
			return resultContent;

		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static HttpEntity createFormEntity(Map<String,String> map) throws UnsupportedEncodingException{
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		for(String key:map.keySet()){
			formparams.add(new BasicNameValuePair(key, map.get(key)));
		}
		UrlEncodedFormEntity  uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); 
		return uefEntity;
	}
	
	
	public static String doPostForm(Map<String, String> map, String url) throws UnsupportedEncodingException {
		List<NameValuePair> formparams = new ArrayList<NameValuePair>();
		for (String key : map.keySet()) {
			String val = map.get(key);
			formparams.add(new BasicNameValuePair(key, val));
		}
		UrlEncodedFormEntity uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
		String str = HttpHelper.doPost(url, uefEntity);
		return str;
	}
	
	/**
	 * utf-8编码
	 */
	static class Utf8ResponseHandler implements ResponseHandler<String> {
		public String handleResponse(final HttpResponse response)
				throws IOException {
			final StatusLine statusLine = response.getStatusLine();
			final HttpEntity entity = response.getEntity();
			if (statusLine.getStatusCode() >= 300) {
				EntityUtils.consume(entity);
				throw new HttpResponseException(statusLine.getStatusCode(),
						statusLine.getReasonPhrase());
			}
			return entity == null ? null : EntityUtils
					.toString(entity, "UTF-8");
		}

	}

}
